
package com.rx.mgt.controller;  

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rx.mgt.domain.StatisticsInfo;
import com.rx.mgt.po.UsersBo;
import com.rx.mgt.service.StatisticsInfoService;

/** 
 * ClassName:StatisticsInfoController 
 * Function :
 * Reason   : 
 * Date     :Feb 7, 2017 9:26:06 AM 
 * @author   luolei 
 * @since    JDK 1.6
 */
@Controller
public class StatisticsInfoController {
    @Autowired
    StatisticsInfoService statisticsInfoService;
    @RequestMapping(value="/charts-flow.htmlx")
    public String  chartsFlow(ModelMap model,UsersBo usersBo) throws ParseException{
        Calendar a=Calendar.getInstance();
        int year = a.get(Calendar.YEAR);
        if(usersBo.getYear()>0){
            year= usersBo.getYear();
        }
        SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
        usersBo.setType("flow");
        List<StatisticsInfo> list =  statisticsInfoService.getStatisticsInfoCount(usersBo, year);
        String result="";
        for(int i=0;i<12;i++){
            boolean hasData=false;
            for(int j=0;j<list.size();j++){
                if(i==list.get(j).getMonth()-1){
                    result=result+list.get(j).getTotal();
                    hasData=true;
                }
            }
            if(!hasData){
                result=result+"0";
            }
            if(i<11){
                result=result+",";
            }
        }
        model.addAttribute("result",result);
        return "chart/charts-flow";
    }
   
    
    
    @RequestMapping(value="/charts-user-add.htmlx")
    public String  chartsUserAdd(ModelMap model,UsersBo usersBo) throws ParseException{
        Calendar a=Calendar.getInstance();
        int year = a.get(Calendar.YEAR);
        if(usersBo.getYear()>0){
            year= usersBo.getYear();
        }
        usersBo.setType("userAdd");
      //  List<StatisticsInfo> list =  statisticsInfoService.getStatisticsInfoCount(usersBo, year);
        List<Map<String,Object>> alist = statisticsInfoService.getUserAddInfoMations(year);
        String result="";
        for(int i=0;i<12;i++){
            boolean hasData=false;
            for(int j=0;j<alist.size();j++){
                Map<String,Object> rs =  (Map<String, Object>) alist.get(j);
                String  m= (String)rs.get("mounth");
                String m0 = m.substring(5);
                int m1 =0;
                if(m0.contains("0")){
                    m1 = Integer.valueOf(m0.substring(1));
                }
                if(i==m1-1){
                    result=result+rs.get("count");;
                    hasData=true;
                }
            }
            if(!hasData){
                result=result+"0";
            }
            if(i<11){
                result=result+",";
            }
        }
        model.addAttribute("result",result);
        return "chart/charts-user-add";
    }
    
   
    @RequestMapping(value="/charts-user-login.htmlx")
    public String  chartsUserLogin(ModelMap model,UsersBo usersBo) throws ParseException{
        Calendar a=Calendar.getInstance();
        int year = a.get(Calendar.YEAR);
        if(usersBo.getYear()>0){
            year= usersBo.getYear();
        }
        usersBo.setType("login");
        List<StatisticsInfo> list =  statisticsInfoService.getStatisticsInfoCount(usersBo, year);

        String result="";
        for(int i=0;i<12;i++){
            boolean hasData=false;
            long staal =0;
            for(int j=0;j<list.size();j++){
                if(i==list.get(j).getMonth()-1){
                    staal+=list.get(j).getTotal();
                    hasData=true;
                }
            }
            if(hasData){
                result=result+staal;
            }
            if(!hasData){
                result=result+"0";
            }
            if(i<11){
                result=result+",";
            }
        }
        model.addAttribute("result",result);
        return "chart/charts-user-login";
    }
    @RequestMapping(value="/charts-virus.htmlx")
    public String  chartsVirus(ModelMap model,UsersBo usersBo) throws ParseException{
        Calendar a=Calendar.getInstance();
        int year = a.get(Calendar.YEAR);
        if(usersBo.getYear()>0){
            year= usersBo.getYear();
        }
        usersBo.setType("virus");
        List<StatisticsInfo> list =  statisticsInfoService.getStatisticsInfoCount(usersBo, year);
        String result="";
        for(int i=0;i<12;i++){
            boolean hasData=false;
            for(int j=0;j<list.size();j++){
                if(i==list.get(j).getMonth()-1){
                    result=result+list.get(j).getTotal();
                    hasData=true;
                }
            }
            if(!hasData){
                result=result+"0";
            }
            if(i<11){
                result=result+",";
            }
        }
        model.addAttribute("result",result);
        return "chart/charts-virus";
    }
    @RequestMapping(value="/charts-volume.htmlx")
    public String  chartsVolume(ModelMap model,UsersBo usersBo) throws ParseException{
        Calendar a=Calendar.getInstance();
        int year = a.get(Calendar.YEAR);
        if(usersBo.getYear()>0){
            year= usersBo.getYear();
        }
        usersBo.setType("volume");
       // List<StatisticsInfo> list =  statisticsInfoService.getStatisticsInfoCount(usersBo, year);
        List<Map<String,Object>> rsList = statisticsInfoService.queryWpVolume(year);

        String result="";
        for(int i=0;i<12;i++){
            boolean hasData=false;
            for(int j=0;j<rsList.size();j++){
                Map<String,Object> rs =rsList.get(j);
                String dateStr = (String)rs.get("datestr");
                String month = dateStr.substring(5);
                int m1 = 0;
                if(month.contains("0")){
                    m1 =Integer.valueOf(month.substring(1));
                }
                if(i==m1-1){
                    float size = ((BigDecimal)rs.get("size")).floatValue();
                    DecimalFormat format = new DecimalFormat("0.00");
                    String szrs = format.format(size/1024/1024);
                    result=result+szrs;
                    hasData=true;
                }
            }
            if(!hasData){
                result=result+"0";
            }
            if(i<11){
                result=result+",";
            }
        }
        model.addAttribute("result",result);
        return "chart/charts-volume";
    }
}
  